package com.garygyp.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.garygyp.commonutils.JwtUtils;
import com.garygyp.commonutils.MD5;
import com.garygyp.servicebase.exceptionhandler.GuliException;
import com.garygyp.ucenter.entity.Member;
import com.garygyp.ucenter.entity.RegisterVo;
import com.garygyp.ucenter.mapper.MemberMapper;
import com.garygyp.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author GaryGyp
 * @since 2022-05-17
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    // 登录
    @Override
    public String login(Member member) {
        // 1. 获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        // 手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败");
        }

        // 判断手机号是否正确
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Member mobileMember = baseMapper.selectOne(wrapper);
        // 判断查出来的对象是否为空
        if (mobileMember == null) {
            // 数据库没有这个手机号
            throw new GuliException(20001, "登录失败，该用户尚未注册");
        }
        // 判断密码
        // 因为存储到数据库的密码一定是加密的
        // 所以要把我们输入的密码先进性加密，再与数据库中的密码进行比较
        // 加密方式：MD5（只能加密，不能解密）
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(20001, "登录失败，密码错误");
        }
        // 判断用户是否被禁用
        if (mobileMember.getIsDisabled()) {
            throw new GuliException(20001, "登录失败，该用户已被禁用");
        }

        // 登录成功
        // 生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return jwtToken;
    }

    // 注册
    @Override
    public void register(RegisterVo registerVo) {
        // 获取注册的数据
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        // 非空判断
        // 手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname)) {
            throw new GuliException(20001, "注册失败");
        }

        // TODO 判断邮箱验证码是否正确

        // 相同判断，手机号不能重复
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "注册失败,用户已存在");
        }

        // 注册用户,把数据添加到数据库中
        Member member = new Member();
        member.setMobile(registerVo.getMobile());
        member.setNickname(registerVo.getNickname());
        member.setPassword(MD5.encrypt(registerVo.getPassword()));
        member.setIsDisabled(false);
        member.setAvatar("https://garygyp-edufile.oss-cn-hangzhou.aliyuncs.com/2022/05/10/868db2c6b1404b6d8a91f574cfeb1249file.png");
        baseMapper.insert(member);
    }

    // 根据openid查询
    @Override
    public Member getOpenIdMember(String openid) {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.eq("openid", openid);
        Member member = baseMapper.selectOne(wrapper);
        return member;
    }

    // 5. 查询某一天的注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
